package com.junmiao.A;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class StockUtils {

	public static void mergeSort(String fileName1, String fileName2, String mergeFile) throws IOException
	{
		int count = 1;
		
		File f1 = new File(fileName1);
		File f2 = new File(fileName2);
		File f3 = new File(mergeFile);
		
	    FileReader fr1 = new FileReader(f1);
	    FileReader fr2 = new FileReader(f2);
	    FileWriter fw = new FileWriter(f3);
	    
	    BufferedReader br1 = new BufferedReader(fr1);
	    BufferedReader br2 = new BufferedReader(fr2);
	    BufferedWriter bw = new BufferedWriter(fw);
	    
	    ArrayList<String> al1 = new ArrayList<String>();
	    ArrayList<String> al2 = new ArrayList<String>();
	    ArrayList<String> al3 = new ArrayList<String>();
	    
	    String s1,s2;
	    while((s1=br1.readLine())!=null){
	    	//s1 =  gbToUtf8(s1);
	    	System.out.println(s1);
	    	al1.add(s1);
	    }
	    
	    while((s2=br2.readLine())!=null){
	    //	s2 =  gbToUtf8(s2);
	    	al2.add(s2);
	    }
	    
	    while(al1.size()>0||al2.size()>0){
	    	if(al1.size()>0&&al2.size()>0){
	    		double d1 = getPercent(al1.get(0));
	    		double d2 = getPercent(al2.get(0));
	    		bw.write(count + " : ");
	    		if(d1>=d2){
	    			bw.write(al1.get(0) + "\n");
	    			al1.remove(0);
	    		}else{
	    			bw.write(al2.get(0) + "\n");
	    			al2.remove(0);
	    		}
	    	}else if(al1.size()>0){
	    		bw.write(al1.get(0) + "\n");
	    		al1.remove(0);
	    	}else if(al2.size()>0){
	    		bw.write(al2.get(0)+ "\n");
	    		al1.remove(0);
	    	}
	    	count++;	
	    }
	    
	    bw.flush();
	    bw.close();
	    fw.close();
	    
	    br1.close();
	    br2.close();
	    
	    fr1.close();
	    fr2.close();
	    
	    
	}
	
	public static String gbToUtf8(String str) throws UnsupportedEncodingException {   
        StringBuffer sb = new StringBuffer();   
        for (int i = 0; i < str.length(); i++) {   
            String s = str.substring(i, i + 1);   
            if (s.charAt(0) > 0x80) {   
                byte[] bytes = s.getBytes("Unicode");   
                String binaryStr = "";   
                for (int j = 2; j < bytes.length; j += 2) {   
                    // the first byte   
                    String hexStr = getHexString(bytes[j + 1]);   
                    String binStr = getBinaryString(Integer.valueOf(hexStr, 16));   
                    binaryStr += binStr;   
                    // the second byte   
                    hexStr = getHexString(bytes[j]);   
                    binStr = getBinaryString(Integer.valueOf(hexStr, 16));   
                    binaryStr += binStr;   
                }   
                // convert unicode to utf-8   
                String s1 = "1110" + binaryStr.substring(0, 4);   
                String s2 = "10" + binaryStr.substring(4, 10);   
                String s3 = "10" + binaryStr.substring(10, 16);   
                byte[] bs = new byte[3];   
                bs[0] = Integer.valueOf(s1, 2).byteValue();   
                bs[1] = Integer.valueOf(s2, 2).byteValue();   
                bs[2] = Integer.valueOf(s3, 2).byteValue();   
                String ss = new String(bs, "UTF-8");   
                sb.append(ss);   
            } else {   
                sb.append(s);   
            }   
        }   
        return sb.toString();   
    }   
  
    private static String getHexString(byte b) {   
        String hexStr = Integer.toHexString(b);   
        int m = hexStr.length();   
        if (m < 2) {   
            hexStr = "0" + hexStr;   
        } else {   
            hexStr = hexStr.substring(m - 2);   
        }   
        return hexStr;   
    }   
  
    private static String getBinaryString(int i) {   
        String binaryStr = Integer.toBinaryString(i);   
        int length = binaryStr.length();   
        for (int l = 0; l < 8 - length; l++) {   
            binaryStr = "0" + binaryStr;   
        }   
        return binaryStr;   
    }   
	
	public static double getPercent(String stock)
	{
		double percent = -1;
		
		String[] a = stock.split(":");
		if(a.length == 3)
		{
			a[2] = a[2].trim();
		    percent = Double.parseDouble(a[2]);
		}
		return percent;
	}
	
	public static int countOrg(StockA stock)
	{
		int i=0;
		
		if(stock!=null){
			
			ArrayList<ShareHolder> shs = stock.getShareHolders();
		
			for(int t=0;t<10;t++)
			{
				ShareHolder sh = shs.get(t);
		
				if(sh.getName().length()>5&&((sh.getDelta()).indexOf("新进")>=0||(sh.getDelta()).indexOf("+")>=0))
				{
					
					i += 1;
				}
			}
		}
		
		return i;
	}
}
